// pages/check_list/check_list.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disable: false,
    lists: [],
    code: '',
    checkTaskRContents: [],
    deviceId: '',
    deviceClassificationId: '',
    checkTaskId: '',
    checkStandardId: '',
    platform: '',
    selected: 1,
    chooseList: [],
    listselected: '1',
    list: [{
        name: '指示灯',
        id: 1,
        sublist: [{
            name: '指示灯正常',
            id: 222,
            status: false,
          },
          {
            name: '目视检查',
            id: 222,
            status: false,
          }
        ]
      },
      {
        name: '柜体及外观',
        id: 1,
        sublist: [{
            name: '无异味',
            id: 222,
            status: false,
          },
          {
            name: '无打火烧痕',
            id: 222,
            status: false,
          }
        ]
      }

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.data.deviceId = options.id;
    }
    if (options.checkTaskId) {
      this.data.checkTaskId = options.checkTaskId;
    }
    if (options.deviceClassificationId) {
      this.data.deviceClassificationId = options.deviceClassificationId;
    }
    if (options.checkname) {
      this.data.checkname = options.checkname;
    }
    if (options.code) {
      this.data.code = options.code;
    }

    this.setData(this.data);
    this.loadData();
  },
  // 选择
  selectsataus: function (e) {

    var that = this;
    // if (!that.data.disable){
    //   wx.showToast({
    //     title: '请先扫码确认是否是任务设备',
    //     icon: 'none',
    //     duration: 3000
    //   })
    //   return
    // }

    var id = e.currentTarget.dataset.cid;

    // var item = this.data.vehicleTypeItems[index]
    // item.isSelected = !item.isSelected;
    // var chooseList = [];
    var choosecheckStandardId = '';
    var n = 0;

    for (var i in that.data.lists) {
      var lista = that.data.lists[i];
      // 
      for (var j in lista.checkContentVOs) {
        // 

        var pro = lista.checkContentVOs;

        if (pro[j].id == id) {

          if (pro[j].contentStatus == 1) {
            pro[j].contentStatus = 2;


          } else {
            pro[j].contentStatus = 1;
          }

          let index = that.data.chooseList.findIndex((obj, i, objs) => {
            return obj.checkStandardId == lista.id && obj.checkContentId == pro[j].id
          });

          if (index != -1) {
            that.data.chooseList[index].contentStatus = pro[j].contentStatus;
          } else {
            that.data.chooseList.push({
              'checkStandardId': lista.id,
              'checkContentId': pro[j].id,
              'contentStatus': pro[j].contentStatus
            });
          }




          // choosecheckStandardId = choosecheckStandardId + lista.id + ",";
          // if (that.data.chooseList.length > 0) {
          //   for (var k in that.data.chooseList) {
          //     var clist = that.data.chooseList;
          //     
          //     
          //     
          //     if (clist[k].checkStandardId != id && pro[j].id == id){

          //       that.data.chooseList.push({
          //         'checkStandardId': lista.id,
          //         'checkContentId': pro[j].id,
          //         'contentStatus': pro[j].contentStatus
          //       });

          //     }
          //   }
          // }else{
          //   that.data.chooseList.push({
          //     'checkStandardId': lista.id,
          //     'checkContentId': pro[j].id,
          //     'contentStatus': pro[j].contentStatus
          //   });
          // }


        }
      }
    }
    // that.data.checkStandardId = choosecheckStandardId;
    // that.data.checkTaskRContents = chooseList;
    that.setData(that.data);
    // 

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  selected: function (e) {
    var t = e.currentTarget.dataset.t;
    this.setData({
      selected: t
    })
  },
  listselected1: function (e) {
    var t = e.currentTarget.dataset.t;

    this.setData({
      listselected: t,
      navNumber: t
    })

  },
  listselected: function (e) {
    var t = e.currentTarget.dataset.t;

    this.setData({
      listselected: 0,
      navNumber: 0
    })
  },
  scan: function () {
    var that = this;
    wx.scanCode({
      success(res) {


        if (res.result == that.data.code) {
          that.setData({
            disable: true
          })
        } else {
          wx.showToast({
            title: '任务设备和当前设备不匹配，请选择正确设备',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail() {
        wx.showToast({
          title: '扫描失败',
          icon: 'fail',
          duration: 3000
        })
      }
    })
  },
  hqbtn() {
    wx.showToast({
      title: '已缓存',
      icon: 'fail',
      duration: 2000
    });
  },
  submitPra: function () {
    var that = this;
    if (!that.data.chooseList.length > 0) {
      wx.showToast({
        title: '您尚未修改，请修改后保存',
        icon: 'none',
        duration: 2000
      })
      return
    }


    var arra = that.data.chooseList.reverse();
    var arrb = Array.from(new Set(arra));
    var data = {
      'checkTaskId': that.data.checkTaskId,
      'deviceId': that.data.deviceId,
      'checkTaskRContents': arrb
    }
    wx.request({
      url: applet_park_api_url + '/api/checkContents',
      method: 'PUT',
      header: {
        contentType: "application/json",
      },
      data: JSON.stringify(data),
      success: function (res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.loadData();

          prevPage.setData(prevPage.data);
          wx.navigateBack();
          // wx.redirectTo({
          //   url: '../patrol_record/patrol_record',
          // })

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络异常',
        });
      }

    })
  },
  loadData() {
    var that = this;


    wx.request({
      url: applet_park_api_url + '/api/checkStandards',
      method: 'get',

      data: {
        "checkTaskId": that.data.checkTaskId,
        "deviceId": that.data.deviceId,
        'deviceClassificationId': that.data.deviceClassificationId
      },
      success: function (res) {

        if (res.data.code == 0) {
          var lists = res.data.data;

          that.setData({
            lists: lists
          })

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {}

    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})